<template>
	<view>
		<button v-if="user == null" open-type="getPhoneNumber" @getphonenumber="getPhone">授权登录</button>
    <view v-if="user != null">
      <image style="width: 60px;height: 60px;" mode="aspectFill" :src="user.avatar" />
      <view>欢迎您，{{user.nickname ? user.nickname : user.phone}}</view>
      <button @click="getUserInfo">授权个人信息</button>
    </view>
	</view>
</template>

<script>
	import { getUserData, setUserData } from '@/utils'
	import { mpLogin, profile, mpUserInfo } from '@/api/my'
	export default {
		data() {
			return {
				user: null,
        code: '',
			}
		},
		async onShow() {
      await this.checkLogin()
      if (!this.user) {
        let that = this
        // 先调用login获取code 而不是在getPhoneNumber的时候获取 可避免小概率后端解密问题
        uni.login({
          success(loginRes) {
            console.log('uniLogin', loginRes)
            that.code = loginRes.code
          },
          fail(loginErr) {
            console.log('uniLogin', loginErr)
          }
        })
      }
		},
		methods: {
      async checkLogin() {
        this.user = getUserData()
        console.log(this.user)
        if (!this.user) {
          this.user = null
          return
        }
        try {
          const info = await profile()
          for (const key in info.data) {
            this.user[key] = info.data[key]
          }
          setUserData(this.user)
        }catch(checkLogin) {
          console.log('checkLogin', checkLogin)
          this.user = null
        }
        
        
      },
		  getPhone(e) {
        console.log(e)
        if (e.detail.encryptedData && e.detail.iv) {
          uni.showLoading()
          
          if (this.code) {
            mpLogin({
              code: this.code,
              encrypted_data: e.detail.encryptedData,
              iv: e.detail.iv,
            }).then(mpLoginRes => {
              console.log('mpLoginRes', mpLoginRes)
              this.user = mpLoginRes.data
              setUserData(this.user)
              uni.hideLoading()
            }).catch(mpLoginErr => {
              console.log('mpLoginErr', mpLoginErr)
            })
          }
          
        }
      },
      getUserInfo() {
        let that = this
        uni.getUserProfile({
        	desc: '授权个人信息',
        	success(user) {
            console.log(user)
            uni.showLoading()
            mpUserInfo({
              nickname: user.userInfo.nickName,
              avatar: user.userInfo.avatarUrl
            }).then(userInfoRes => {
              console.log('userInfoRes', userInfoRes)
              for (const key in userInfoRes.data) {
                that.user[key] = userInfoRes.data[key]
              }
              setUserData(that.user)
              uni.hideLoading()
            }).catch(userInfoErr => {
              console.log('userInfoErr', userInfoErr)
            })
          },
          fail(err) {
            console.log(err)
          }
        })
      },
		}
	}
</script>

<style>
  
</style>
